package com.ybwh.springboot1.conf;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//@Configuration
//@EnableSwagger2
public class SwaggerConfig {
	 @Bean
	    public Docket createRestApi() {
//	        return new Docket(DocumentationType.SWAGGER_2)
//	                .apiInfo(apiInfo())
//	                .select()
//	                .apis(RequestHandlerSelectors.basePackage("com.ybwh.springboot1"))
//	                .paths(PathSelectors.any())
//	                .build()
//	                .groupName("cloud-teacher-api");
	        
	        
	        return new Docket(DocumentationType.SWAGGER_2)
	                .apiInfo(apiInfo())
	                .globalOperationParameters(operationParameters())
	                .genericModelSubstitutes(DeferredResult.class)
	                .useDefaultResponseMessages(false)
//	                .forCodeGeneration(true)
	                .pathMapping("/")// base，最终调用接口后会和paths拼接在一起
	                .select()
	                .apis(RequestHandlerSelectors.basePackage("com.ybwh.springboot1"))
	                .build();
	        
	    }

	    private ApiInfo apiInfo() {
	        return new ApiInfoBuilder()
	                .title("项目标题")
	                .description("项目描述")
	                .termsOfServiceUrl("http://blog.didispace.com/")
	                .contact(new Contact("cashZhang", "https://blog.csdn.net/m0_37726449", "cashzhang27@qq.com"))
	                .license("The Apache License, Version 2.0")
	                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
	                .version("1.1.1")
	                .build();
	    }
	    
	    /**
	     * 全局配置参数说明
	     *
	     * @return
	     */
	    private List<Parameter> operationParameters() {
	        ParameterBuilder tokenPar = new ParameterBuilder();
	        List<Parameter> params = new ArrayList<Parameter>();
	        tokenPar.name("token").description("登录令牌")
	                .modelRef(new ModelRef("string"))
	                .parameterType("header")
	                .required(false).build();
	        params.add(tokenPar.build());
	        return params;
	    }
}
